function [reme,r_p,r_s,r_c] = Q3f_1()

    global remember;
    global r_pos;
    global r_str;
    global r_cost;
    global Defective_rate_first;
    global Defective_rate_mid;
    global Defective_rate_fg;
    global yield_first;
    global yield_mid;
    global yield_fg;
    r_cost = zeros(10,1);
    r_pos = 1;
    profit = 0;   % 生产一单位的成品总共的利润 
    r_str = [" "," "," "," "," "," "," "," "," "," "];
    str = '';
    cost(1,profit,str);
    reme = remember;
    r_p = r_pos;
    r_s = r_str;
    r_c = r_cost;
end
% 基本零件
function [] = cost(pos,profit,str)
    global first_data;
    global Defective_rate_first;
    global yield_first;
    % 对于每种基本零件来说，有检测与不检测两种情况

    % 不进行检测
    profit_new = profit - first_data(pos,2);
    Defective_rate_first(pos) = first_data(pos,1);
    sstr = str + "  零件" + num2str(pos) + "不进行检测"+num2str(profit_new);
    yield_first(pos) = 1;
    if(pos == 9)
        cost_tmp(1,profit_new,sstr)
    else
        cost(pos  + 1,profit_new,sstr)
    end

    % 进行检测
    profit_new = profit - first_data(pos,2) - first_data(pos,3);
    Defective_rate_first(pos) = 0;
    sstr = str + "   零件" + num2str(pos) + "进行检测"+num2str(profit_new);
    yield_first(pos) = 1- first_data(pos,1);
    if(pos == 9)
        cost_tmp(1,profit_new,sstr);
    else
        cost(pos + 1,profit_new,sstr);
    end
end
% 半成品
function [] = cost_tmp(pos,profit,str)
    global mid_data;
    global first_data;
    global Defective_rate_mid;
    global Defective_rate_first;
    global yield_first;
    global yield_mid;
    % 对于每种半成品来说有 1 不检测 2 检测并回收  3 检测并丢弃 
    
    % 不进行检测
    yield_mid(pos) = min( ...
    min( yield_first((pos-1)*3 + 1),yield_first((pos-1)*3 + 2) ), ...
    yield_first((pos-1)*3 + 3) );
    
    profit_new = profit - mid_data(pos,2)*yield_mid(pos);
    
    Defective_rate_mid(pos) = 1 - (1 -  Defective_rate_first((pos-1)*3 + 1))*...
    (1 -  Defective_rate_first((pos-1)*3 + 2))*...
    (1 -  Defective_rate_first((pos-1)*3 + 3))*...
    (1 - mid_data(pos,1));
    
    sstr = str + "   半成品" + num2str(pos) + "不进行检测"+num2str(profit_new);
    if(pos == 3)
        cost_fg(profit_new,sstr);
    else
        cost_tmp(pos + 1,profit_new,sstr);
    end

    % 进行检测但不回收
    Defective_rate_mid(pos) = 0;
    profit_new = profit - (mid_data(pos,2) + mid_data(pos,3)) * yield_mid(pos);
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    yield_mid(pos) = min( ...
    min( yield_first((pos-1)*3 + 1),yield_first((pos-1)*3 + 2) ), ...
    yield_first((pos-1)*3 + 3) )* ...
    (1 - Defective_rate_first((pos-1)*3 + 1))* ...
    (1 - Defective_rate_first((pos-1)*3 + 2))* ...
    (1 - Defective_rate_first((pos-1)*3 + 3))* ...
    (1 - mid_data(pos,1));


    sstr = str + "   半成品" + num2str(pos) + "检测不回收"+num2str(profit_new) ;
    if(pos == 3)
        cost_fg(profit_new,sstr);
    else
        cost_tmp(pos+1,profit_new,sstr);
    end

    % 进行检测并回收
    tmp = 1 - (1 -  Defective_rate_first((pos-1)*3 + 1))*...
    (1 -  Defective_rate_first((pos-1)*3 + 2))*...
    (1 -  Defective_rate_first((pos-1)*3 + 3))*...
    (1 - mid_data(pos,1));
    profit_new = profit_new - tmp * mid_data(pos,4)*(yield_mid(pos));
    
    PFirstOk = (1 - Defective_rate_first((pos-1)*3 + 1)) * (1 - ...
        (1 - Defective_rate_first((pos-1)*3 + 2))* ...
        (1 - Defective_rate_first((pos-1)*3 + 3))* ...
        (1 - mid_data(pos,1)  ) ...
    ) * yield_mid(pos);
    PSecondOk = (1 - Defective_rate_first((pos-1)*3 + 2)) * (1 - ...
        (1 - Defective_rate_first((pos-1)*3 + 1))* ...
        (1 - Defective_rate_first((pos-1)*3 + 3))* ...
        (1 - mid_data(pos,1)  ) ...
    ) * yield_mid(pos);   
    PThirdOk = (1 - Defective_rate_first((pos-1)*3 + 3)) * (1 - ...
        (1 - Defective_rate_first((pos-1)*3 + 2))* ...
        (1 - Defective_rate_first((pos-1)*3 + 1))* ...
        (1 - mid_data(pos,1)  ) ...
    ) * yield_mid(pos);
    profit_new = profit_new + ...
        PFirstOk * (first_data((pos-1)*3 + 1,2) +first_data((pos-1)*3 + 1,3)) + ...
        PSecondOk * (first_data((pos-1)*3 + 2,2) +first_data((pos-1)*3 + 2,3)) + ...
        PThirdOk * (first_data((pos-1)*3 + 3,2) +first_data((pos-1)*3 + 3,3));
    sstr = str + "   半成品" + num2str(pos) + "检测并回收"+num2str(profit_new) ;
    if(pos == 3)
        cost_fg(profit_new,sstr);
    else
        cost_tmp(pos + 1,profit_new,sstr);
    end
end

% 成品
function [] = cost_fg(profit,str)
    global fg_data;    
    global mid_data;
    global first_data;
    global Defective_rate_fg;
    global Defective_rate_mid;
    global Defective_rate_first;
    global yield_first;
    global yield_mid;
    global yield_fg;
    % 对于成品来说有 1 不检测 2 检测并回收  3 检测并丢弃 
    
    % 不进行检测
    profit_new = profit - fg_data(2)*(min(yield_mid(1),min(yield_mid(2),yield_mid(3) )));
    Defective_rate_fg = 1 - (1 -  Defective_rate_mid(1))*...
    (1 -  Defective_rate_mid(2))*...
    (1 -  Defective_rate_mid(3))*...
    (1 - fg_data(1));

    yield_fg = min( ...
    min( yield_mid(1),yield_mid( 2 ) ), ...
    yield_mid( 3) );
    
    sstr = str + "成品不进行检测";
    sell(profit_new,sstr);


     % 检测不回收
    profit_new = profit - (fg_data(2) +fg_data(3))*(min(yield_mid(1),min(yield_mid(2),yield_mid(3))));
    Defective_rate_fg = 0;
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    yield_fg = min( ...
    min( yield_mid(1),yield_mid( 2 ) ), ...
    yield_mid( 3) )* ...
    (1 - Defective_rate_mid(1))* ...
    (1 - Defective_rate_mid(2))* ...
    (1 - Defective_rate_mid(3))* ...
    (1 - Defective_rate_fg);
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    sstr = str + "  成品检测不回收"+num2str(profit);
    sell(profit_new,sstr);



%     % 检测+回收
%     profit_new = profit - fg_data(2);
%     %这里需要一个近似的结果来计算拆解出零件的费用
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    tmp = 0;
    tt =min( ...
    min( yield_mid(1),yield_mid( 2 ) ), ...
    yield_mid( 3) )*(1 - (1 -  Defective_rate_mid(1))*...
    (1 -  Defective_rate_mid(2))*...
    (1 -  Defective_rate_mid(3))*...
    (1 - fg_data(1)));
    for pos =(1:9)
        tmp = tmp + (first_data(pos,2) + first_data(pos,3)) * tt*(1 - Defective_rate_first(pos));
    end
    for pos =(1:3)
        tmp = tmp - (mid_data(pos,4)) * tt*(1 - Defective_rate_mid(pos));
    end
    tmp = tmp - fg_data(4) * Defective_rate_fg;
    profit_new = profit +tmp;
    sstr = str + "  成品检测+回收"+num2str(profit_new)+" 获得"+num2str(tmp);
    sell(profit_new,sstr);
end


function [] = sell(profit,str)
    global yield_fg;
    global end_data;
    str = str + "生产出" + num2str(yield_fg);
    profit = profit + yield_fg * end_data(1);
    cost_after_sales(profit,str);
end


% 售后
function [] = cost_after_sales(profit,str)
    global remember;
    global r_pos;
    global r_str;
    global r_cost;
    global fg_data;    
    global Defective_rate_fg;
    global yield_fg;
    global end_data;
    global first_data;
    global mid_data;

    global Defective_rate_fg;
    global Defective_rate_mid;
    global Defective_rate_first;
    % 首先所有不合格商品进行调换 ,原价减去  调换花费乘成品坏的概率
    % 是否进行拆解
    % 减去调换费用
    profit = profit - (end_data(2) + 148)* Defective_rate_fg;
    str = str + "不合格进行报废"+ num2str(profit)+"  ";

    fprintf("%s 利润为%s \n",str,num2str(profit));

    remember(r_pos) = profit;
    r_pos= r_pos + 1;
    if(r_cost(1) < profit)
        addmax(profit,str);
    end
    % 对不合格的商品拆解时可能会获得合格的零件
    % 如果合格的零件费用大于拆解花费的话,相当于获利

    % 次品率 :1,2,成品 的基本次品率相互独立
    % 零件1不出错但是成品出错
    % 1 单位成品,不合格Defective_rate(3)个

    % 1 单位成品,保证某零件合格
 %这里需要一个近似的结果来计算拆解出零件的费用
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    
    % 加上新零件的费用
     tmp = 0;
    tt =(1 - (1 -  Defective_rate_mid(1))*...
    (1 -  Defective_rate_mid(2))*...
    (1 -  Defective_rate_mid(3))*...
    (1 - fg_data(1)));
    for pos =(1:9)
        tmp = tmp + (first_data(pos,2) + first_data(pos,3)) * tt*(1 - Defective_rate_first(pos));
    end
    for pos =(1:3)
        tmp = tmp - (mid_data(pos,4)) * tt*yield_fg*(1 - Defective_rate_mid(pos));
    end
    profit = profit +tmp;
    str = str + "不合格进行回收"+ num2str(profit);

    fprintf("%s 利润为%s \n",str,num2str(profit));
    remember(r_pos) = profit;
    r_pos= r_pos + 1;
    % if(r_cost < profit)
    %     r_cost = profit;
    %     r_str = sstr;
    % end
end


function [] = addmax(profit,str)
    pos = 1;
    global r_str;
    global r_cost;
    while pos < 10
        pos = pos + 1;
        if profit > r_cost(pos)
            r_cost(pos - 1) = r_cost(pos); 
            r_str(pos - 1) = r_str(pos); 
        else
            r_cost(pos - 1) = profit; 
            r_str(pos - 1) = str; 
            return;
        end
    
    end
    if(pos == 10)
        r_cost(pos ) = profit; 
        r_str(pos) = str; 
    end

end


















